package phoenix.web.page_objects.features;

import org.openqa.selenium.By;
import org.testng.TestException;
import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseWebExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.web.locators.base.features.NaughtyModeLocatorsBase;
import phoenix.web.test_objects.data_model.NaughtyMode;

/**
 * Created by AlexGolovko on 27.11.2014.
 */
public class NaughtyModePage extends PageBaseWebExt
{
	/**
	 * Initializes PageBaseWebExt
	 * @param p_manager PhoenixManager instance
	 */
	public NaughtyModePage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	public NaughtyModeLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_CLICKANDFLIRT:
			{
				return new phoenix.web.locators.clickandflirt.features.NaughtyModeLocators();
			}
			case ConfigExt.SITE_ID_LOCALSGOWILD:
			{
				return new phoenix.web.locators.localsgowild.features.NaughtyModeLocators();
			}
			case ConfigExt.SITE_ID_EBONYFLIRT:
			{
				return new phoenix.web.locators.ebonyflirt.features.NaughtyModeLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.web.locators.cheekylovers.features.NaughtyModeLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.web.locators.wildbuddies.features.NaughtyModeLocators();
			}
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.web.locators.flirt.features.NaughtyModeLocators();
			}
			case ConfigExt.SITE_ID_FOREIGNGIRLFRIEND:
			{
				return new phoenix.web.locators.foreigngirlfriend.features.NaughtyModeLocators();
			}
			case ConfigExt.SITE_ID_BENAUGHTY:
			{
				return new phoenix.web.locators.benaughty.features.NaughtyModeLocators();
			}
			default:
			{
				return new phoenix.web.locators.benaughty.features.NaughtyModeLocators();
			}
		}
	}

	public NaughtyMode getTypeInSearch(TestUserDataModel p_user)
	{
		String p_value = wd().getAttribute(elements().userLink(p_user.getId()), "class");
		return this.getNaughtyType(p_value);
	}

	public NaughtyMode getModeTypeUserProfile()
	{
		String p_value = null;
		if (manager.isFlirt())
		{
			p_value = wd().getAttribute(elements().blockNaughtyType, "class");
		}
		else
		{
			p_value = wd().getAttribute(elements().blockNaughtyType, "data-nm-trigger");
		}
		return this.getNaughtyType(p_value);
	}

	public NaughtyMode getModeTypeNotification()
	{
		wd().waitForPresence(elements().elNotification, "pop-up alert");
		String p_value = wd().getAttribute(elements().elNotification, "class");
		return this.getNaughtyType(p_value);
	}

	public NaughtyMode getTypeMainPhoto()
	{
		String p_value = wd().getAttribute(elements().elMailPhoto, "class");
		return this.getNaughtyType(p_value);
	}

	public NaughtyMode getTypeOfGalleryRoll()
	{
		String p_value = wd().getAttribute(elements().elPhotoList, "class");
		return this.getNaughtyType(p_value);
	}

	private NaughtyMode getNaughtyType(String p_value)
	{
		if (p_value.contains("blurred-heavy"))
		{
			return NaughtyMode.HARD;
		}
		else if (p_value.contains("blurred"))
		{
			return NaughtyMode.SEXY;
		}
		if (p_value.equals("3"))
		{
			return NaughtyMode.HARD;
		}
		else if (p_value.equals("2"))
		{
			return NaughtyMode.SEXY;
		}
		else
		{
			return NaughtyMode.NORMAL;
		}
	}

	public void setNaughtyMode(NaughtyMode p_type)
	{
		log().info("Setting Naughty mode to " + p_type.toString());

		if (manager.isBN() || manager.isWB())
		{
			manager.web().menuPage().clickAcitivityNavigation();
			wd().wait(1); // Some delay for JS
			if (!manager.web().menuPage().isAcitivityNavigationDisplayed())
			{
				throw new TestException("Can't find navigation bar for activity");
			}
		}

		// When the same mode is already set
		String p_currentMode = wd().getText(elements().blockSwitcher);

		if (p_currentMode.trim().toLowerCase().equals("normal") && p_type.equals(NaughtyMode.NORMAL))
		{
			this.actionsForSkip();
			return;
		}
		else if (p_currentMode.trim().toLowerCase().equals("sexy") && p_type.equals(NaughtyMode.SEXY))
		{
			this.actionsForSkip();
			return;
		}
		else if (p_currentMode.trim().toLowerCase().equals("no limits") && p_type.equals(NaughtyMode.HARD))
		{
			this.actionsForSkip();
			return;
		}

		wd().waitForPresence(elements().blockSwitcher, "Navigation mode selector");
		wd().click(elements().blockSwitcher);

		if (!this.isDropDownPresent())
		{
			throw new TestException("Unable to find drop menu");
		}

		By el_link = By.xpath("");

		switch (p_type)
		{
			case NORMAL:
				el_link = elements().linkNormal;
				break;
			case SEXY:
				el_link = elements().linkSexy;
				break;
			case HARD:
				el_link = elements().linkHard;
				break;
		}

		if (manager.isFlirt())
		{
			// Wait for drop down menu
			wd().waitForPresence(elements().blockDropMenu, "Drop down menu");
		}

		wd().waitForPresence(el_link, "Mode setter " + p_type.toString());
		wd().click(el_link);
		wd().wait(5);

		if (this.isGuidelinesPopUpDisplayed())
		{
			wd().waitForPresence(elements().btnSubmit, "Submit pop-up");
			wd().click(elements().btnSubmit);
		}
	}

	private boolean isDropDownPresent()
	{
		return wd().isElementPresent(elements().blockDropMenu);
	}

	private void actionsForSkip()
	{
		if (manager.isBN() || manager.isWB())
		{
			manager.web().menuPage().clickAcitivityNavigationClose();
		}
	}

	public boolean isGuidelinesPopUpDisplayed()
	{
		log().info("Content Browsing Guidelines");
		return wd().isElementDisplayed(By.xpath("//span[contains(.,'Content Browsing Guidelines')]"));
	}
}
